#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>

int main(int argc,char const* argv[])
{
    pid_t pid;
    int pidtemp;
    char buf[32];
    int fd=-1;
    time_t timep;

    fd=open("./fifo",O_WRONLY);
    if(fd<0)
    {
        perror("fifo open err\n");
        return -1;
    }
    while (1)
    {
        pid=fork();
        if(pid<0)
        {
            perror("fork err\n");
            return -1;
        }
        else if(pid==0)
        {
            time(&timep);
            strcpy(buf,ctime(&timep));
            write(fd,buf,strlen(buf)+1);

            pidtemp=getpid();
            sprintf(buf,"%d",pidtemp);
            printf("%s\n",buf);
            write(fd,buf,strlen(buf)+1);

            write(fd,"\n",1);
            exit(0);
        }
        else
        {
            wait(NULL);
        }
        sleep(3);
    }

    close(fd);

    return 0;
}